package util.biz.yzwl.model;

import util.biz.yzwl.URLCon;
import util.model.common.FieldInfo;
import util.model.common.TableInfo;
import util.util.DevComUtil;
import util.util.DevFileUtil;
import util.util.DevModelUtil;

import java.util.List;

/**
 * @author: liW
 * @date: 2020/9/2 17:33
 */
public class CreateSql {

  private final static String SQL_CREATE_PATH = URLCon.SQL_CREATE_PATH;
  public static String MODLOU_OUT_PATH = URLCon.MODLOU_OUT_PATH;

  private static int SHEET_PAGE = 0;
  private final static int START_ROW = 1;

  public static void main(String[] args) {

    int start = 9;
    int length = 1;
    for (int i = start; i < start + length; i++) {
      SHEET_PAGE = i;
            create();
//      batchInsert();
//        insert();
//        update();
//        cloumnAs();
    }
  }


  /**
   * 添加sql
   */
  private static void batchInsert() {
    // 获取字段信息
    TableInfo tableInfo = DevModelUtil.getTableInfo(SQL_CREATE_PATH, SHEET_PAGE, START_ROW);

    List<FieldInfo> mysqlFields = tableInfo.getFieldList();

    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ");
    sb.append(tableInfo.getTableDBName());
    sb.append("\n(");

    int n = mysqlFields.size();
    int i = 0;
    for (FieldInfo mysqlField : mysqlFields) {
      i++;
      sb.append(mysqlField.getFieldDBName());
      if (i < n) {
        sb.append(",");
      }
      sb.append("\n");
    }

    sb.append(")\n");
    sb.append("VALUES \n");
    sb.append("<foreach collection='list' item='item' separator=','>\n");
    sb.append("(\n");
     i = 0;
    for (FieldInfo mysqlField : mysqlFields) {

      String fieldDBName = mysqlField.getFieldDBName();
      String fieldName = DevComUtil.line2Hump(fieldDBName);
//      if (baseBeanMap.containsKey(fieldDBName)) {
//        sb.append(baseBeanMap.get(fieldDBName));
//      } else {
        sb.append("#{");
        sb.append("item.");
        sb.append(fieldName);
        sb.append("}");
//      }
      i++;
      if (i < n) {
        sb.append(",");
      }
      sb.append("\n");
    }

    sb.append(")\n");
    sb.append("</foreach>\n");
    System.out.println(sb.toString());


  }

  /**
   * 创建表sql
   */
  private static void create() {
    // 获取字段信息
    TableInfo tableInfo = DevModelUtil.getTableInfo(SQL_CREATE_PATH, SHEET_PAGE, START_ROW);
    String tableDBName = tableInfo.getTableDBName();

    List<FieldInfo> mysqlFields = tableInfo.getFieldList();

    StringBuilder sb = new StringBuilder();
    for (FieldInfo mysqlField : mysqlFields) {
      sb.append(DevModelUtil.createMysql(mysqlField));
      sb.append("\n");
    }

    sb.append(DevModelUtil.createPk(mysqlFields));
    sb.append("\n");
    String value = "CREATE TABLE `"
        + tableDBName
        + "`  ("
        + sb.toString()
        + ") ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;";
    String tableName = DevModelUtil.dBName2BeanName(tableDBName);
    String outPath = MODLOU_OUT_PATH + "sql\\" + tableDBName + ".sql";
    DevFileUtil.createFile(value, outPath);
  }

  /**
   * 添加sql
   */
  private static void insert() {
    // 获取字段信息
    TableInfo tableInfo = DevModelUtil.getTableInfo(SQL_CREATE_PATH, SHEET_PAGE, START_ROW);

    List<FieldInfo> mysqlFields = tableInfo.getFieldList();

    StringBuilder sb = new StringBuilder();
    for (FieldInfo mysqlField : mysqlFields) {

      sb.append(mysqlField.getFieldDBName());
      sb.append(",\n");
    }

    sb.append("VALUES (\n");

    for (FieldInfo mysqlField : mysqlFields) {

      sb.append("#{");
      String fieldName = mysqlField.getFieldDBName();
      fieldName = DevComUtil.line2Hump(fieldName);
      sb.append(fieldName);
      sb.append("},\n");
    }

    sb.append(")");
    System.out.println(sb.toString());


  }

  /**
   * 修改sql
   */
  private static void update() {
    // 获取字段信息
    TableInfo tableInfo = DevModelUtil.getTableInfo(SQL_CREATE_PATH, SHEET_PAGE, START_ROW);
    List<FieldInfo> mysqlFields = tableInfo.getFieldList();
    StringBuilder sb = new StringBuilder();
    for (FieldInfo mysqlField : mysqlFields) {
      String fieldDBName = mysqlField.getFieldDBName();
      String fieldName = DevComUtil.line2Hump(fieldDBName);
      sb.append(fieldDBName);
      sb.append("=#{");
      sb.append(fieldName);
      sb.append("},\n");
    }
    System.out.println(sb.toString());
  }

  /**
   *
   */
  private static void cloumnAs() {
    // 获取字段信息
    TableInfo tableInfo = DevModelUtil.getTableInfo(SQL_CREATE_PATH, SHEET_PAGE, START_ROW);

    List<FieldInfo> mysqlFields = tableInfo.getFieldList();

    StringBuilder sb = new StringBuilder();

    for (FieldInfo mysqlField : mysqlFields) {
      String fieldDBName = mysqlField.getFieldDBName();
      String fieldName = DevComUtil.line2Hump(fieldDBName);
      sb.append(fieldDBName);
      sb.append(" as ");

      sb.append(fieldName);
      sb.append(",\n");
    }

    System.out.println(sb.toString());


  }

}
